package com.mocoop.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mocoop.activity.R;

@SuppressLint("ResourceAsColor")
public class MenuItem extends RelativeLayout {
	public final static String ERROR = "!", UPLOADING = "↑", ENDUPLOAD = "endupload", BEFORESTATUS = "beforeStatus";
	private TextView mTextView = null, countMessage;
	private ImageView mImageView = null, imgMessage;
	private OnItemClickListener mOnClickListener = null;
	private boolean uploading;
	private String iconStatus = "";
	private TranslateAnimation animation;
	private int smallSize;
	
	public MenuItem(Context context) {
		super(context);
	}

	public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
	
	public MenuItem(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.level_menu_item, this);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LevelMenuItem);
		
		smallSize = dip2px(context,10);
		
		initWidget(typedArray);
	}

	private void initWidget(TypedArray typedArray) {
		mTextView = (TextView) findViewById(R.id.tv_item);
		countMessage = (TextView) findViewById(R.id.count_message);
		imgMessage = (ImageView) findViewById(R.id.img_message);
		
		String textString = typedArray.getString(R.styleable.LevelMenuItem_text);
		int textColor = typedArray.getColor(R.styleable.LevelMenuItem_text_color, 0xffffffff);

		mTextView.setText(textString);
		mTextView.setTextColor(textColor);
		mTextView.setTextSize(10.0f);

		mImageView = (ImageView) findViewById(R.id.image_item);
		int imageSrc = typedArray.getResourceId(R.styleable.LevelMenuItem_image_src, 0);

		mImageView.setAlpha(255);
		mImageView.setImageResource(imageSrc);

		typedArray.recycle();

		 animation = new TranslateAnimation(0,0,15,0);
		animation.setDuration(1200);
		animation.setStartTime(0);
		animation.setRepeatCount(Integer.MAX_VALUE);
		

	}

	public void setCountMsg(String count) {
		if (count.equals(UPLOADING)) {
			beginUploading(count);
			return;
		}

		if (count.equals(ENDUPLOAD)) {
			endUpload();
			return;
		}

		if (uploading) {
			if(!count.equals(ERROR))
			iconStatus = count;
			return;
		}

		if (BEFORESTATUS.equals(count)) {
			
			count = iconStatus;
		}

		if ("".equals(count)) {
			countMessage.setVisibility(View.GONE);
		} else if (" ".equals(count)) {
			iconStatus = count;
			countMessage.getLayoutParams().height = smallSize;
			countMessage.getLayoutParams().width = smallSize;
			countMessage.setGravity(Gravity.CENTER);
			countMessage.setText(count);
			countMessage.setTextSize(0);
			countMessage.setVisibility(View.VISIBLE);
		} else {
			countMessage.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
			countMessage.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
			countMessage.setGravity(Gravity.CENTER);
			if(!count.equals(ERROR))
			iconStatus = count;
			countMessage.setText(count);
			countMessage.setTextSize(12);
			countMessage.setVisibility(View.VISIBLE);
		}
	}

	public void beginUploading(String count) {
		uploading = true;
		imgMessage.startAnimation(animation);
		countMessage.setVisibility(View.GONE);
		imgMessage.setVisibility(View.VISIBLE);
	}

	public void endUpload() {
		imgMessage.clearAnimation();
		imgMessage.setVisibility(View.GONE);
		uploading = false;
	}

	public void hideCountMsg() {
		countMessage.setVisibility(View.GONE);
	}

	public void setText(String text) {
		mTextView.setText(text);
	}

	/**
	 * @param textColor
	 */
	public void setTextColor(int textColor) {
		mTextView.setTextColor(textColor);
	}

	/**
	 * @param textSize
	 */
	public void setTextSize(int textSize) {
		mTextView.setTextSize(textSize);
	}

	public void setImageResource(int resId, boolean isPressed) {
		mImageView.setImageResource(resId);
		if (isPressed) {
			setTextColor(getResources().getColor(R.color.blue));
		} else {
			setTextColor(getResources().getColor(R.color.darker_gray));
		}
	}

	public void setBackgroundResource(int resId) {
		mImageView.setBackgroundResource(resId);
	}

	public void setOnClickListener(OnItemClickListener listener) {
		this.mOnClickListener = listener;
		// this.setOnTouchListener(new View.OnTouchListener() {
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// if(event.getAction()==MotionEvent.ACTION_DOWN){
		// mOnClickListener.onImageClick(v.getId());
		// }
		// return false;
		// }
		// });
		this.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mOnClickListener.onImageClick(v.getId());
			}
		});
	}

	public interface OnItemClickListener {
		public void onImageClick(int id);
	}
}
